package com.tanglongan.concurrent._sync;

import lombok.AllArgsConstructor;

public class Sync04 {
    public static void main(String[] args) throws InterruptedException {
        Sync04User user = new Sync04User();
        Thread t = new Sync04Thread(user);
        t.start();
        Thread.sleep(200);
        user.getValue();
    }
}


class Sync04User {
    private String username = "A";
    private String password = "AA";

    synchronized public void setUsernameAndPassword(String username, String password) {
        this.username = username;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.password = password;
        System.out.println("setUsernameAndPassword方法，线程名称：" + Thread.currentThread().getName() + ",username=" + username + ",password=" + password);
    }

    synchronized public void getValue() {
        System.out.println("getValue方法，线程名称：" + Thread.currentThread().getName() + ",username=" + username + ",password=" + password);
    }
}


@AllArgsConstructor
class Sync04Thread extends Thread {
    private Sync04User user;

    @Override
    public void run() {
        user.setUsernameAndPassword("B", "BB");
    }
}